package com.masterclient.app.businessmodule.vipmodule.financial_management.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.activity.FCBaseActivity;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.vipmodule.financial_management.adapter.BankListAdapter;
import com.masterclient.app.businessmodule.vipmodule.financial_management.bean.BankCardBean;
import com.masterclient.app.businessmodule.vipmodule.financial_management.contract.BankListContract;
import com.masterclient.app.businessmodule.vipmodule.financial_management.contract.BankListPresenter;


import java.util.List;

import butterknife.BindView;
@Route(path = FCRouterPath.BANK_LIST)
public class BankListActivity extends FCBaseActivity<BankListPresenter> implements BankListContract.View {

    @BindView(R.id.bank_list_recycler)
    RecyclerView recyclerView;
    @BindView(R.id.bank_list_confirm)
    TextView bankListConfirm;

    @Autowired(name = "bank_card_id")
    String bankCardId = "";
    @Autowired(name = "fromPageType")
    String fromPageType = "";


    private static final int REQUEST_CODE = 1;
    private BankListAdapter bankListAdapter ;
    private View emptyView;
    private int position = 0;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_bank_list;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("银行卡列表");

    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {

        if (!"setting".equals(fromPageType)){
            bankListConfirm.setText("确定");
        }else {
            bankListConfirm.setText("添加新的银行卡");
        }
        recyclerView.setPadding(0,0,0,ScreenUtils.dp2px(12));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = ScreenUtils.dp2px(4);
            }
        });
        bankListAdapter = new BankListAdapter(fromPageType);

        emptyView = View.inflate(getActivity(),R.layout.view_base_state,null);
        ((ImageView)emptyView.findViewById(R.id.img_base_state)).setImageResource(R.drawable.flower_empty_bank);
        ((TextView)emptyView.findViewById(R.id.tv_base_state)).setText("购物怎能没有银行卡");
        TextView emptyBtn = emptyView.findViewById(R.id.tv_base_state_refresh);
        emptyBtn.setText("添加银行卡");
        emptyBtn.setVisibility(View.VISIBLE);
        emptyBtn.setOnClickListener(v->{
            ARouter.getInstance().build(AroutePath.DEALER_BACK_CARD_INSERT_ACTIVITY).navigation(this,REQUEST_CODE);
        });

        emptyView.setVisibility(View.GONE);
        bankListConfirm.setVisibility(View.GONE);
        bankListAdapter.setEmptyView(emptyView);

        bankListAdapter.bindToRecyclerView(recyclerView);
        bankListAdapter.setOnItemClickListener((BaseQuickAdapter adapter, View view, int position)->{
            if (!"setting".equals(fromPageType)){
                if (position == bankListAdapter.getData().size()-1){
                    ARouter.getInstance().build(AroutePath.DEALER_BACK_CARD_INSERT_ACTIVITY).navigation(this,REQUEST_CODE);
                }else {
                    BankListActivity.this.position = position;
                    bankListAdapter.setBankCardId(bankListAdapter.getData().get(position).bankcardCertId);
                }
            }
        });
        if (!TextUtils.isEmpty(bankCardId)){
            bankListAdapter.setBankCardId(bankCardId);
        }

        bankListConfirm.setOnClickListener(v->{
            if (!"setting".equals(fromPageType)){
                if (TextUtils.isEmpty(bankListAdapter.getBankCardId())) {
                    ToastUtil.showToast("请选择银行卡");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("bank_card",bankListAdapter.getData().get(position));
                setResult(RESULT_OK,intent);
                finish();
            }else {
                ARouter.getInstance().build(AroutePath.DEALER_BACK_CARD_INSERT_ACTIVITY).navigation(this,REQUEST_CODE);
            }
        });
        mPresenter.getData();
    }

    @Override
    public void showData(List<BankCardBean> depositData) {
        if (depositData != null && depositData.size()>0){
            if (!"setting".equals(fromPageType)){
                depositData.add(new BankCardBean());
            }
            bankListAdapter.setNewData(depositData);
            bankListConfirm.setVisibility(View.VISIBLE);
        }else {
            bankListAdapter.getData().clear();
            emptyView.setVisibility(View.VISIBLE);
            bankListConfirm.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            mPresenter.getData();
        }
    }
}
